﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ERP_SaaS_WinForm.采购管理.Server;

namespace ERP_SaaS_WinForm.采购管理
{
    public partial class Details : Form
    {
        private PlanDatas qualityData;
        
        // 声明控件
        private Label lblQuaCode;
        private Label lblQuaName;
        private Label lblQualityInspector;
        private Label lblQuaDate;
        private Label lblLDate;
        private Label lblQuaState;
        private Label lblInsDepName;
        private Label lblQuaRemark;

        
        public Details()
        {
            InitializeComponent();
            InitializeDetailsForm();
        }
        
        /// <summary>
        /// 初始化详情窗体
        /// </summary>
        private void InitializeDetailsForm()
        {
            try
            {
                // 设置窗体基本属性
                this.Text = "质检详情";
                this.Size = new Size(600, 500);
                this.StartPosition = FormStartPosition.CenterParent;
                this.FormBorderStyle = FormBorderStyle.FixedDialog;
                this.MaximizeBox = false;
                this.MinimizeBox = false;
                
                // 创建控件
                CreateControls();
                
                Console.WriteLine("Details窗体初始化完成");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"初始化Details窗体时发生异常: {ex.Message}");
                MessageBox.Show($"初始化窗体失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        
        /// <summary>
        /// 创建详情窗体控件
        /// </summary>
        private void CreateControls()
        {
            try
            {
                // 创建标题标签
                Label titleLabel = new Label();
                titleLabel.Text = "质检详细信息";
                titleLabel.Font = new Font("微软雅黑", 16, FontStyle.Bold);
                titleLabel.ForeColor = Color.DarkBlue;
                titleLabel.Location = new Point(20, 20);
                titleLabel.Size = new Size(300, 30);
                titleLabel.TextAlign = ContentAlignment.MiddleLeft;
                this.Controls.Add(titleLabel);
                
                // 创建分隔线
                Panel separatorLine = new Panel();
                separatorLine.BackColor = Color.LightGray;
                separatorLine.Location = new Point(20, 60);
                separatorLine.Size = new Size(540, 2);
                this.Controls.Add(separatorLine);
                
                // 创建详情信息标签
                int startY = 90;
                int labelHeight = 25;
                int spacing = 35;
                
                // 质检编号
                CreateDetailLabel("质检编号:", "", 30, startY, ref lblQuaCode);
                startY += spacing;
                
                // 质检名称
                CreateDetailLabel("质检名称:", "", 30, startY, ref lblQuaName);
                startY += spacing;
                
                // 质检人员
                CreateDetailLabel("质检人员:", "", 30, startY, ref lblQualityInspector);
                startY += spacing;
                
                // 质检部门
                CreateDetailLabel("质检部门:", "", 30, startY, ref lblInsDepName);
                startY += spacing;
                
                // 质检日期
                CreateDetailLabel("质检日期:", "", 30, startY, ref lblQuaDate);
                startY += spacing;
                
                // 来料日期
                CreateDetailLabel("来料日期:", "", 30, startY, ref lblLDate);
                startY += spacing;
                
                // 质检状态
                CreateDetailLabel("质检状态:", "", 30, startY, ref lblQuaState);
                startY += spacing;
                
                // 质检备注
                CreateDetailLabel("质检备注:", "", 30, startY, ref lblQuaRemark);
                startY += spacing;
                
                // 创建关闭按钮
                Button closeButton = new Button();
                closeButton.Text = "关闭";
                closeButton.Size = new Size(80, 35);
                closeButton.Location = new Point(250, startY + 20);
                closeButton.BackColor = Color.LightGray;
                closeButton.FlatStyle = FlatStyle.Flat;
                closeButton.Click += CloseButton_Click;
                this.Controls.Add(closeButton);
                
                Console.WriteLine("Details窗体控件创建完成");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"创建控件时发生异常: {ex.Message}");
                MessageBox.Show($"创建控件失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        
        /// <summary>
        /// 创建详情标签
        /// </summary>
        /// <param name="labelText">标签文本</param>
        /// <param name="valueText">值文本</param>
        /// <param name="x">X坐标</param>
        /// <param name="y">Y坐标</param>
        /// <param name="valueLabel">值标签引用</param>
        private void CreateDetailLabel(string labelText, string valueText, int x, int y, ref Label valueLabel)
        {
            try
            {
                // 标签
                Label label = new Label();
                label.Text = labelText;
                label.Font = new Font("微软雅黑", 10, FontStyle.Bold);
                label.ForeColor = Color.DarkBlue;
                label.Location = new Point(x, y);
                label.Size = new Size(100, 25);
                label.TextAlign = ContentAlignment.MiddleLeft;
                this.Controls.Add(label);
                
                // 值
                valueLabel = new Label();
                valueLabel.Text = valueText;
                valueLabel.Font = new Font("微软雅黑", 10);
                valueLabel.ForeColor = Color.Black;
                valueLabel.Location = new Point(x + 120, y);
                valueLabel.Size = new Size(300, 25);
                valueLabel.TextAlign = ContentAlignment.MiddleLeft;
                valueLabel.BorderStyle = BorderStyle.FixedSingle;
                valueLabel.BackColor = Color.White;
                this.Controls.Add(valueLabel);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"创建详情标签时发生异常: {ex.Message}");
            }
        }
        
        /// <summary>
        /// 关闭按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CloseButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        
        /// <summary>
        /// 设置质检数据（供外部调用）
        /// </summary>
        /// <param name="data">质检数据</param>
        public void SetQualityData(PlanDatas data)
        {
            try
            {
                this.qualityData = data;
                
                if (qualityData != null)
                {
                    // 设置窗体标题
                    this.Text = $"质检详情 - {qualityData.quaCode}";
                    
                    // 更新各个标签的值
                    UpdateDetailLabels();
                    
                    Console.WriteLine($"Details窗体已设置质检数据: {qualityData.quaCode}");
                    
                    // 显示数据设置成功的消息
                    MessageBox.Show($"质检详情数据已设置！\n质检编号：{qualityData.quaCode}\n质检名称：{qualityData.quaName}", 
                        "设置成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"设置质检详情时发生异常: {ex.Message}");
                MessageBox.Show($"设置质检详情时发生错误: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        
        /// <summary>
        /// 更新详情标签的值
        /// </summary>
        private void UpdateDetailLabels()
        {
            try
            {
                if (qualityData == null) return;
                
                // 更新各个标签的值
                if (lblQuaCode != null) lblQuaCode.Text = qualityData.quaCode ?? "";
                if (lblQuaName != null) lblQuaName.Text = qualityData.quaName ?? "";
                if (lblQualityInspector != null) lblQualityInspector.Text = qualityData.qualityInspector ?? "";
                if (lblInsDepName != null) lblInsDepName.Text = qualityData.insDepName ?? $"部门ID: {qualityData.quaDepId}";
                if (lblQuaDate != null) lblQuaDate.Text = qualityData.quaDate.ToString("yyyy-MM-dd");
                if (lblLDate != null) lblLDate.Text = qualityData.lDate.ToString("yyyy-MM-dd");
                if (lblQuaState != null) lblQuaState.Text = qualityData.quaState ? "已合格" : "未合格";
                if (lblQuaRemark != null) lblQuaRemark.Text = qualityData.quaRemark ?? "无";
                
                Console.WriteLine("详情标签值更新完成");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"更新详情标签值时发生异常: {ex.Message}");
            }
        }
        
        /// <summary>
        /// 窗体加载事件
        /// </summary>
        private void Details_Load(object sender, EventArgs e)
        {
            Console.WriteLine("Details窗体加载完成");
        }
    }
}